/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * catatTransaksiKembali.java
 *
 * Created on Nov 10, 2011, 11:26:40 PM
 */
package rentalmobil.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import rentalmobil.entity.dataMobil;
import rentalmobil.entity.kembali_mobil;

/**
 *
 * @author Henfryandie
 */
public class catatTransaksiKembali extends java.awt.Dialog {

    kembali_mobil km = new kembali_mobil();
    List<kembali_mobil> listOut = new ArrayList<kembali_mobil>();
    dataMobil dm = new dataMobil();

    /** Creates new form catatTransaksiKembali */
    public catatTransaksiKembali(java.awt.Frame parent, boolean modal) throws SQLException{
        super(parent, modal);
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    
        this.isiTabelKembali();
    }

    boolean cekteks() {
        if (!txtNoSewa.getText().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    void hapusTextField() {
        txtNoSewa.setText(null);
        txtNmPenyewa.setText(null);
        txtalmPenyewa.setText(null);
        txtJam.setText(null);
        txtTglSewa.setText(null);
        txtKemb.setText(null);
        txtNoMob.setText(null);
    }

    void isiTabelKembali() throws SQLException {
        listOut = km.tampilKembaliMobil();
        Object data[][] = new Object[listOut.size()][9];
        int x = 0;
        for (kembali_mobil bk : listOut) {
            data[x][0] = bk.getNo_kembali();
            data[x][1] = bk.getNo_sewa();
            data[x][2] = bk.getNamaPenyewa();
            data[x][3] = bk.getAlamatPenyewa();
            data[x][4] = bk.getJaminan();
            data[x][5] = bk.getTanggalSewa();
            data[x][6] = bk.getTanggalKembali();
            data[x][7] = bk.getTanggalMobilKembali();
            data[x][8] = bk.getNo_plat_mobil();
            x++;
        }
        String[] judul = {"No Kembali", "No Sewa", "Nama", "Alamat", "Jaminan", "Tanggal Sewa", "Tanggal Kembali", "Tanggal Mobil Kembali","No Mobil"};
        tabelKembali.setModel(new DefaultTableModel(data, judul));
        jScrollPane1.setViewportView(tabelKembali);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtNoSewa = new javax.swing.JTextField();
        btnCekSewa = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtNmPenyewa = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtalmPenyewa = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtJam = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtTglSewa = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtKemb = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtNoMob = new javax.swing.JTextField();
        btnSimpan = new javax.swing.JButton();
        jButtonKeluar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        dateMobilHarusKembali = new com.toedter.calendar.JDateChooser();
        btnReset = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelKembali = new javax.swing.JTable();

        setResizable(false);
        setTitle("Form Transaksi Kembali");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jPanel2.setBackground(new java.awt.Color(153, 255, 153));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("Catat Transaksi Kembali");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(295, 295, 295)
                .addComponent(jLabel1)
                .addContainerGap(297, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        jPanel3.setBackground(new java.awt.Color(153, 255, 153));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transaksi Kembali Mobil", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel2.setText("No Sewa");

        btnCekSewa.setText("Cek Sewa");
        btnCekSewa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCekSewaActionPerformed(evt);
            }
        });

        jLabel3.setText("Nama Penyewa");

        txtNmPenyewa.setEditable(false);

        jLabel4.setText("Alamat Penyewa");

        txtalmPenyewa.setEditable(false);

        jLabel5.setText("Jaminan");

        txtJam.setEditable(false);

        jLabel6.setText("Tanggal Sewa");

        txtTglSewa.setEditable(false);

        jLabel7.setText("Tanggal Kembali");

        txtKemb.setEditable(false);

        jLabel8.setText("No Mobil");

        txtNoMob.setEditable(false);

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        jButtonKeluar.setText("Keluar");
        jButtonKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonKeluarActionPerformed(evt);
            }
        });

        jLabel9.setText("Tanggal Pengembalian");

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addGap(29, 29, 29)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtNoSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCekSewa))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnReset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonKeluar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtalmPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNmPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNoMob, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtKemb, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtTglSewa, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(dateMobilHarusKembali, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(txtJam, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(277, 277, 277))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnReset, btnSimpan});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNoSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCekSewa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNmPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtalmPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(txtJam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTglSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtKemb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(dateMobilHarusKembali, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtNoMob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSimpan)
                    .addComponent(btnReset)
                    .addComponent(jButtonKeluar))
                .addContainerGap())
        );

        tabelKembali.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No Kembali", "No Sewa", "Nama Penyewa", "Alamat Penyewa", "Jaminan", "Tanggal Sewa", "Tanggal Kembali", "Title 8", "No Mobil"
            }
        ));
        tabelKembali.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tabelKembaliAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(tabelKembali);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btnCekSewaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCekSewaActionPerformed
        try {
            if (cekteks()) {
                km.getTransaksiSewa(Integer.parseInt(txtNoSewa.getText()));
                if (km.getNamaPenyewa() == null) {
                    JOptionPane.showMessageDialog(this, "Data tidak tersedia");
                } else {
                    txtNmPenyewa.setText(km.getNamaPenyewa());
                    txtalmPenyewa.setText(km.getAlamatPenyewa());
                    txtJam.setText(km.getJaminan());
                    txtTglSewa.setText(km.getTanggalSewa().toString());
                    txtKemb.setText(km.getTanggalKembali().toString());
                    txtNoMob.setText(km.getNo_plat_mobil());
                }
            } else {
                JOptionPane.showMessageDialog(this, "Kolom kosong");
                txtNmPenyewa.setText("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(catatTransaksiKembali.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnCekSewaActionPerformed

    private void jButtonKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonKeluarActionPerformed
        this.setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonKeluarActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        km.setNo_sewa(Integer.parseInt(txtNoSewa.getText()));
        km.setTanggalMobilKembali(new java.sql.Date(dateMobilHarusKembali.getDate().getTime()));
        km.setNo_plat_mobil(txtNoMob.getText());
        try {
            km.kembaliMobilInsert();
            dm.updateStatusMobilKembali(txtNoMob.getText());
            JOptionPane.showMessageDialog(this, "Data dengan nomor sewa " + km.getNo_sewa() + " Telah Berhasil di Simpan");
            hapusTextField();
            isiTabelKembali();
        } catch (SQLException ex) {
            Logger.getLogger(catatTransaksiKembali.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void tabelKembaliAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tabelKembaliAncestorAdded

    }//GEN-LAST:event_tabelKembaliAncestorAdded

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
    this.hapusTextField();
    }//GEN-LAST:event_btnResetActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                catatTransaksiKembali dialog = null;
                try {
                    dialog = new catatTransaksiKembali(new java.awt.Frame(), true);
                } catch (SQLException ex) {
                    Logger.getLogger(catatTransaksiKembali.class.getName()).log(Level.SEVERE, null, ex);
                }
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                       System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCekSewa;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSimpan;
    private com.toedter.calendar.JDateChooser dateMobilHarusKembali;
    private javax.swing.JButton jButtonKeluar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelKembali;
    private javax.swing.JTextField txtJam;
    private javax.swing.JTextField txtKemb;
    private javax.swing.JTextField txtNmPenyewa;
    private javax.swing.JTextField txtNoMob;
    private javax.swing.JTextField txtNoSewa;
    private javax.swing.JTextField txtTglSewa;
    private javax.swing.JTextField txtalmPenyewa;
    // End of variables declaration//GEN-END:variables
}
